package com.car.carsharing.service.impl;

import com.car.carsharing.dto.TableListDto;
import com.car.carsharing.mapper.CarMapper;
import com.car.carsharing.mapper.UserDeatilMapper;
import com.car.carsharing.mapper.UserMapper;
import com.car.carsharing.pojo.Car;
import com.car.carsharing.pojo.User;
import com.car.carsharing.pojo.UserDeatil;
import com.car.carsharing.service.UserDeatilService;
import com.car.carsharing.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.relational.core.sql.In;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class UserDeatilServiceImpl implements UserDeatilService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserDeatilMapper userDeatilMapper;
    @Autowired
    private CarMapper carMapper;

    @Override
    public Integer userDeatil(Map map) {
        if(!StringUtils.isEmpty(map)){
            return -1;
        }
        UserDeatil userDeatil = new UserDeatil();
        userDeatil.setUserId((Integer) map.get("userId"));


        UserDeatil ud = userDeatilMapper.selectUserDetail(userDeatil);

        userDeatil.setIdCard((String) map.get("idCard"));
        userDeatil.setName((String) map.get("name"));
        userDeatil.setSex((String) map.get("sex"));
        userDeatil.setCardType((String) map.get("cardType"));
        userDeatil.setUserLicense((String) map.get("userLicense"));

        if(ud == null){
            return userDeatilMapper.insert(userDeatil);
        }

        return userDeatilMapper.update(userDeatil);

    }

    @Override
    public TableListDto getUserList(Map map) {
        Integer pageSize = (Integer) map.get("pageSize");
        if(pageSize == null || "".equals(pageSize)){
            pageSize = 10;
        }
        Integer pageNum = (Integer) map.get("pageNum");
        if(pageNum == null || "".equals(pageNum)){
            pageNum = 1;
        }


        UserDeatil userDeatil =   new UserDeatil();

        userDeatil.setName((String) map.get("name"));
        userDeatil.setIdCard((String) map.get("idCard"));

        PageHelper.startPage(pageNum,pageSize);
        List<UserDeatil> userDeatils = userDeatilMapper.selectUserDetailList(userDeatil);
        PageInfo<UserDeatil> pageInfo = new PageInfo(userDeatils);
        return new TableListDto().sucess(userDeatils,"查询成功",pageInfo.getTotal());
    }

    @Override
    public int updateUser(UserDeatil userDeatil) {

        User user = new User();
        user.setId(userDeatil.getUserId());
        user.setUsername(userDeatil.getUsername());
        user.setPassword(userDeatil.getPassword());

        userMapper.update(user);

        return userDeatilMapper.update(userDeatil);
    }

    @Override
    public int deleteUser(UserDeatil userDeatil) {
        userMapper.delete(userDeatil.getUserId());
        if(userDeatil.getId() == null || "".equals(userDeatil.getId())){
            userDeatil.setId(-1);
        }
        return userDeatilMapper.delete(userDeatil.getId());
    }
}
